<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


/*
Sample Code 497: How to display a flat file database in treeview with dynamically created GtkListStore model - Part 1? 
*/

class field_grid extends listview {

	function __construct($glade,$name,$callback) {

		parent::__construct($glade,$name,$callback);

		$this->columns();
	}

	function columns() {

//print_r($cols);

		$this->col_map = array(
								'id'		=> 0,
								'tag'		=> 1,
								'name'	=> 2,
								'unit'	=> 3,
								'description'	=> 4,
								);

	
		$model_def = array();
		$model_def[] = "GObject::TYPE_STRING";
		$model_def[] = "GObject::TYPE_STRING";
		$model_def[] = "GObject::TYPE_STRING";
		$model_def[] = "GObject::TYPE_STRING";
		$model_def[] = "GObject::TYPE_STRING";

		$this->model = new GtkListStore(); // note 5
		$this->model->set_column_types($model_def); // note 6 

		// setup GtkTreeModelSort and initialise to sort by col 0
		$this->sort_column = 0;
		$this->sort_asc = TRUE;
		$this->modelsort = new GtkTreeModelSort($this->model);  // note 1
		$this->modelsort->set_sort_column_id($this->sort_column, Gtk::SORT_ASCENDING); // note 2 

		// custom sort functions for different data types
/*
		$i = 0;
		foreach($cols['cols'] as $col => $dat) {
			if ( $dat['native_type'] == 'LONGLONG' ) {
				$this->modelsort->set_sort_func($i, array($this,'sort_number')); 
			}
			$i++;
		}
*/
		
		$this->widget->set_model($this->modelsort);

		// Add the column headings
		$col = -1;

		$col++;
		$colname = 'Id';
		$renderer = new GtkCellRendererText();
		$column = new GtkTreeViewColumn($colname, $renderer, 'text', $col);
        $column->set_cell_data_func($renderer, array($this,"row_colours")); // note 1 
 		$column->set_resizable(TRUE);
		$column->set_sort_column_id($col);
		$column->set_visible(FALSE);
        $column->connect('clicked', array($this,'on_click_heading'), $col, $colname); // note 1 
		$this->widget->append_column($column);

		$col++;
		$colname = 'Code';
		$renderer = new GtkCellRendererText();
		$column = new GtkTreeViewColumn($colname, $renderer, 'text', $col);
        $column->set_cell_data_func($renderer, array($this,"row_colours")); // note 1 
 		$column->set_resizable(TRUE);
		$column->set_sort_column_id($col);
        $column->connect('clicked', array($this,'on_click_heading'), $col, $colname); // note 1 
		$this->widget->append_column($column);

		$col++;
		$colname = 'Name';
		$renderer = new GtkCellRendererText();
		$column = new GtkTreeViewColumn($colname, $renderer, 'text', $col);
        $column->set_cell_data_func($renderer, array($this,"row_colours")); // note 1 
 		$column->set_resizable(TRUE);
		$column->set_sort_column_id($col);
        $column->connect('clicked', array($this,'on_click_heading'), $col, $colname); // note 1 
		$this->widget->append_column($column);

		$col++;
		$colname = 'Units';
		$renderer = new GtkCellRendererText();
		$column = new GtkTreeViewColumn($colname, $renderer, 'text', $col);
        $column->set_cell_data_func($renderer, array($this,"row_colours")); // note 1 
 		$column->set_resizable(TRUE);
		$column->set_sort_column_id($col);
        $column->connect('clicked', array($this,'on_click_heading'), $col, $colname); // note 1 
		$this->widget->append_column($column);

		$col++;
		$colname = 'Description';
		$renderer = new GtkCellRendererText();
		$column = new GtkTreeViewColumn($colname, $renderer, 'text', $col);
        $column->set_cell_data_func($renderer, array($this,"row_colours")); // note 1 
 		$column->set_resizable(TRUE);
		$column->set_sort_column_id($col);
        $column->connect('clicked', array($this,'on_click_heading'), $col, $colname); // note 1 
		$this->widget->append_column($column);


	}


	/**
	*
	*	
	*	@param	
	*	@return	
	*	
	*/
	function get_selected($col_code) {
		return(parent::get_selected($this->col_map[$col_code]));
	}



	/**
	*
	*	
	*	@param	
	*	@return	
	*	
	*/
	function set_selected($col_code,$value) {
		return(parent::set_selected($value,$this->col_map[$col_code]));
	}

	/**
	*
	*	
	*	@param	
	*	@return	
	*	
	*/
	function set_sort($col_code,$ascending=TRUE) {
		return(parent::set_sort($this->col_map[$col_code],$ascending));
	}





}



?>